//
//  MyView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2024/12/26.
//

import SwiftUI
//统计标签
struct StatisticsTagView: View {
    @EnvironmentObject var themeManager: ThemeManager
    @StateObject var viewModel = StatisticsTagViewModel()
    @State private var selectId = ""

    var body: some View {
        VStack(alignment: .leading, spacing: 15) {
            if !viewModel.status.isEmpty {
                HomeEmptyView(text1: viewModel.status)
            } else {
                classsifyView
                    .padding(.horizontal, 30)
                pageView
                Spacer()
            }
        }.frame(maxWidth: .infinity,maxHeight: .infinity)
            .onReceive(viewModel.$status) {_ in
                if viewModel.isInit {
                    self.selectId = viewModel.items.first?.objectId ?? ""
                }
            }
            .onAppear{
                if !viewModel.isInit {
                    viewModel.getData(true)
                    viewModel.isInit = true
                }
            }
    }
}

extension StatisticsTagView {
    //标签分类
    var classsifyView: some View {
        ScrollView(.horizontal,showsIndicators: false) {
            VStack(alignment: .leading, spacing: 0) {
                //滚动试图（通过HStack监听selectIndex 变化，实现滚动标签）
                ScrollViewReader { value in
                    //横向布局
                    HStack(spacing: 10) {
                        ForEach(viewModel.items, id: \.objectId) { item in
                            //文本
                            Text(item.text ?? "")
                                .font(.system(size: 12))
                                .foregroundColor(themeManager.themeColor)
                                .padding(.horizontal, 5)
                                .padding(.vertical, 2)
                                .background(themeManager.themeColor.opacity(0.2))
                                .cornerRadius(5)
                                .opacity((item.objectId ?? "") == self.selectId ? 1 : 0.6)
                                .contentShape(Rectangle())
                                .onTapGesture {
                                    withAnimation {
                                        self.selectId = item.objectId ?? ""
                                    }
                                }
                            
                        }
                    }.onChange(of: selectId) { newValue in
                        let selectIndex = viewModel.items.firstIndex(where: { $0.objectId == newValue })
                        withAnimation(Animation.linear(duration: 2)) { //加个动画，标题滚动到中间才不会生硬
                            value.scrollTo(selectIndex,anchor: .center)
                        }
                    }
                }
            }
        }
        
    }
}

extension StatisticsTagView{
    private var pageView : some View {
        
        TabView(selection: $selectId) { //使用selection 就会跟$selectIndex绑定
            ForEach(viewModel.items, id: \.objectId) { item in
                showLog("selectId: \(String(describing: selectId))")
                showLog("\(String(describing: item.objectId))")
                return StatisticsTagListView(bean: item).tag(item.objectId ?? "")
            }
            
        }.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))//使用PageTabViewStyle风格
    }
}

//#Preview {
//    StatisticsView()
//}

//定义预览提供者
struct StatisticsTagView_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: StatisticsTagView())
    }
}
